//
//  DbUtility.m
//  MarketDataAnalyzer
//
//  Created by jiro on 10/01/24.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "DbUtility.h"


@implementation DbUtility

+ (NSString*) convertIsoStdDateStringToCocoaDateString: (NSString*) s
{
	//	末尾の３文字の前に空白を入れゼロを追加		"1000-01-12 03:12:21+09" -> "1000-01-12 03:12:21 +0900"
	NSString* ret =
		[ NSString stringWithFormat:
			@"%@ %@00",
			[ s substringToIndex: 19 ],
			[ s substringFromIndex: 19 ] ];
	return ret;
}

+ (NSDate*) convertIsoStdDateCStringToNSDate: (const char*) s
{
	//	末尾の３文字の前に空白を入れゼロを追加		"1000-01-12 03:12:21+09" -> "1000-01-12 03:12:21 +0900"
	id nss = [ NSString stringWithCString: s encoding: NSUTF8StringEncoding ];
	NSDate* ret =
		[ NSDate dateWithString:
			[ NSString stringWithFormat:
				@"%@ %@00",
				[ nss substringToIndex: 19 ],
				[ nss substringFromIndex: 19 ] ] ];
	return ret;
}

+ (NSDate*) convertIsoStdDateStringToNSDate: (NSString*) s
{
	//	末尾の３文字の前に空白を入れゼロを追加		"1000-01-12 03:12:21+09" -> "1000-01-12 03:12:21 +0900"
	NSDate* ret =
		[ NSDate dateWithString:
			[ NSString stringWithFormat:
				@"%@ %@00",
				[ s substringToIndex: 19 ],
				[ s substringFromIndex: 19 ] ] ];
	return ret;
}

+ (double) parseAsDouble: (PGresult*) res atRowIndex: (int) rowIndex andColumnIndex: (int) colmnIndex
{
	double value = [ [ NSString stringWithCString: PQgetvalue( res, rowIndex, colmnIndex ) encoding: NSUTF8StringEncoding ] doubleValue ];
	return value;
}

+ (double) parseAsDouble: (const char*) s
{
	double value = [ [ NSString stringWithCString: s encoding: NSUTF8StringEncoding ] doubleValue ];
	return value;
}

+ (NSDate*) parseAsNSDate: (const char*) s
{
	if ( s == nil ) { return nil; }
	return [ DbUtility convertIsoStdDateStringToNSDate: [ NSString stringWithFormat: @"%s", s ] ];
}

@end
